package office.service.finance.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import office.action.finance.form.LoanForm;
import office.bo.finance.impl.Loan;
import office.bo.hr.impl.CheckFolw;
import office.bo.system.Department;
import office.domainset.BizDomainProviderSetImpl;
import office.service.finance.ILoanService;
import office.service.system.ISystemService;
import platform.common.exception.BusinessException;
public class LoanServiceImpl extends CheckFolwServiceImpl implements ILoanService{
	/**
	 * 获取贷款列表
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public List loanList(LoanForm objctForm) throws BusinessException{
		List listData = new ArrayList();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Map map = new HashMap();
			map.put("userId", objctForm.getEntryUserId());
			map.put("checkRole", objctForm.getCheckRole());
			if(null==objctForm.getCheckRole() || ("").equals(objctForm.getCheckRole())){
				throw new BusinessException("用户未设置相关角色!");
			}else{
				map.put("deptId", systemService.getDepartment(objctForm.getDeptId(),objctForm.getCompanyId()));
				listData = domainProviderSetImpl.getDao().findByHqlNameAndDomian(domainProviderSetImpl.getLoanDP(), "findByUserIdCheckRoleDept",map);
			}
			
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取贷款列表信息失败!", e);
		}
		return listData;
	}

	/**
	 * 编辑贷款信息
	 * @throws BusinessException
	 */
	public void editLoan(LoanForm objctForm)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Loan bo = null;
			if(null==objctForm.getId() || ("").equals(objctForm.getId())){
				bo = (Loan)domainProviderSetImpl.getLoanDP().newEmptyObj();
			}else{
				bo = (Loan)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getLoanDP(), objctForm.getId());
			}
			bo =(Loan) objctForm.copyPropertiesToBO(bo);// 拷贝了申请数量、描述
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
				bo.setUserId(objctForm.getEntryUserId());
				bo.setState("0");//等待审批
				Calendar c = Calendar.getInstance();
				SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd");
				bo.setEntryDate(dateFmt.format(c.getTime()));
				bo.setEntryRole(objctForm.getEntryRole());
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("保存贷款信息失败!", e);
		}
	}
	

	/**
	 * 根据id获取对象
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public LoanForm getById(String id)throws BusinessException{
		LoanForm form = new LoanForm();//判断form的id属性是否存在,不存在则暂时不存在辞职申请
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			//根据用户信息获取辞职申请,没有获取到数据说明不存在辞职申请,新建辞职申请
			Map map = new HashMap();
			map.put("id", id);
			List list = domainProviderSetImpl.getDao().findByHqlNameAndDomian(domainProviderSetImpl.getLoanDP(), "findById", map);
			if(list.size()>0){
				form = (LoanForm)list.get(0);
			}
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取贷款信息失败!", e);
		}
		return form;
	}
	/**
	 * 根据id删除对象
	 * @param id
	 * @throws BusinessException
	 */
	public void deleteById(String id)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			domainProviderSetImpl.getDao().deleteById(domainProviderSetImpl.getLoanDP(), id);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id删除贷款信息对象失败!", e);
		}
	}
	
	@Override
	public CheckFolw getCheckFolwById(String id) throws BusinessException {
		Loan loan = null;
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			loan = (Loan)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getLoanDP(), id);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("设置审批对象信息失败!", e);
		}
		return loan;
	}
	@Resource
	private ISystemService systemService;
	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}
}
